var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0,
    index:-1,
  },
  //选择地址
  bindaddress: function () {
    wx.navigateTo({
      url: '/pages/addressList/addressList?userId='+wx.getStorageSync("userId")
      })
  },
  
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var type = options.type;
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight
    })
    that.setData({
      price: options.price,
      thumbnailImage: options.thumbnailImage,
      point: options.point,
      title: options.title,
      productIdAndNumList: options.productIdAndNumList,
      type: options.type,
      orderId: options.orderId,
      orderstatus: options.orderstatus,
      ordernumber: options.ordernumber,
      updatetime: options.updatetime,
      opid: options.opid,
      trackingnumber: options.trackingnumber,
    })
    if (options.trackingnumber == undefined) {
      that.setData({
        trackingnumber:''
      })
    }
    console.info(that.data.orderstatus);
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    if (that.data.type == 1) {
      wx.request({
        url: app.globalData.url + 'userGetOrderDetailPage',
        data: {
          orderId: that.data.orderId
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          that.setData({
            username: res.data.data.contactUsername,
            phone: res.data.data.contactTelephone,
            address: res.data.data.contactAddress,
            orderStatus: res.data.data.orderStatus,
          })
        },
        fail: function (err) {
        },
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    var that = this;
    
    
  },

  addtion: function(e){
   
  },

  subtraction: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index; // 获取data- 传进来的index
    var arr = that.data.pAnd;
    if (arr[index].num > 1){
      arr[index].num--;
      that.setData({
        pAnd: arr
      });
      var priceCount = 0;
      var point = 0;
      for (var i = 0; i < arr.length; ++i) {
        priceCount += arr[i].num * arr[i].price;
        point += arr[i].num * arr[i].point;
      }
      that.setData({
        price: parseFloat(priceCount.toFixed(2)),
        price1: parseFloat(priceCount.toFixed(2)),
        price3: parseFloat(priceCount.toFixed(2)),
        point: point
      })
    }

  },
  
 
  youhui:function(){
    
  },
  onShowTextare() {       //显示textare
    this.setData({
      isShowText: false,
      onFacus: true
    })
  },
  onShowText() {       //显示text
    this.setData({
      isShowText: true,
      onFacus: true
    })
  },
  onRemarkInput(event) {               //保存输入框填写内容
    var value = event.detail.value;
    this.setData({
      remark: value,
    });
  },
  navBack: function () {
    wx.navigateBack({
      data:1
    })
  },
  recipient:function(e) {
    var that = this;
    that.setData({
      username : e.detail.value
    })
  }, 
  phone: function (e) {
    var that = this;
    that.setData({
      phone: e.detail.value
    })
  }, 
  address: function (e) {
    var that = this;
    that.setData({
      address: e.detail.value
    })
  }, 
  remark: function (e) {
    var that = this;
    that.setData({
      remark: e.detail.value
    })
  }, 
  submit: function () {
    var that = this;
    var userId = wx.getStorageSync("userId");
    console.info(that.data.type)
    if (that.data.type == 0 || that.data.type == undefined) {
      if (that.data.username == '' || that.data.username == undefined) {
        wx.showToast({
          title: '请输入收件人姓名',
          icon: 'none',
          duration: 2000
        });
      } else if (that.data.phone == '' || that.data.phone == undefined) {
        wx.showToast({
          title: '请输入手机号',
          icon: 'none',
          duration: 2000
        });
      } else if (!(/^1[3456789]\d{9}$/.test(that.data.phone))) {
        wx.showToast({
          title: '手机号格式不正确',
          icon: 'none',
          duration: 2000
        });
      } else if (that.data.address == '' || that.data.address == undefined) {
        wx.showToast({
          title: '请输入收货地址',
          icon: 'none',
          duration: 2000
        });
      } else {
        wx.navigateTo({
          url: '/pages/cashierIntegral/cashierIntegral?userId=' + userId + '&username=' + that.data.username + '&telephone=' + that.data.phone + '&address=' + that.data.address + '&remark=' + that.data.remark + '&productIdAndNumList=' + that.data.productIdAndNumList + '&type=' + 4 + '&payChannel=' + 5003 + '&orderId' + that.data.orderId + '&ptype=' + that.data.type + '&price=' + that.data.point+'积分+'+that.data.price+'元'
        })
        // wx.request({
        //   url: app.globalData.url + "submitOrder",
        //   data: {
        //     userId: userId,
        //     username: that.data.username,
        //     telephone: that.data.phone,
        //     address: that.data.address,
        //     remark: that.data.remark,
        //     productIdAndNumList: that.data.productIdAndNumList,
        //     type: 4,
        //     payChannel: 5003
        //   },
        //   header: {
        //     'content-type': 'application/json'
        //   },
        //   success: function (res) {
        //     var data = res.data.data;
        //     if (res.data.result==0) {
        //       wx.showToast({
        //         title: res.data.errmsg,
        //         icon: 'none'
        //       })
        //       return;
        //     }
        //     if (data.orderStatus != 7001) {
        //       // that.modal_s();
        //       that.setData({
        //         userId: userId,
        //         orderId: data.orderId,
        //       })
        //       wx.request({
        //         url: app.globalData.url + 'getOrderWeixinPrepayId',
        //         data: {
        //           userId: userId,
        //           orderId: data.orderId,
        //           orderNumber: data.orderNumber,
        //         },
        //         method: "POST",
        //         header: {
        //           'content-type': 'application/x-www-form-urlencoded'
        //         },
        //         success: function (res) {
        //           var resultData = res.data;
        //           var hisId = resultData.hisId;
        //           if (res.data.result != 1 && res.data.result != 9) {
        //             wx.showToast({
        //               title: "数据异常，请稍后重试",
        //               icon: 'none'
        //             })
        //             return;
        //           }
        //           wx.requestPayment({
        //             'timeStamp': resultData.timeStamp,
        //             'nonceStr': resultData.nonceString,
        //             'package': resultData.packageStr,
        //             'signType': resultData.signType,
        //             'paySign': resultData.sign,
        //             //支付成功
        //             success: function (res) {
        //               if ("requestPayment:ok" === res.errMsg) {
        //                 wx.showToast({
        //                   title: '支付成功',
        //                   icon: 'success',
        //                   duration: 2000,
        //                   success: function () {
        //                     setTimeout(function () {
        //                       wx.navigateBack({
                                
        //                       });
        //                     }, 2000) //延迟时间
        //                   }
        //                 })
        //               }
        //             },
        //             //支付或者取消失败
        //             fail: function (res) {
        //               if ("requestPayment:fail cancel" === res.errMsg) {
        //                 wx.showToast({
        //                   title: '取消支付',
        //                   icon: 'none',
        //                   duration: 2000,
        //                   success: function () {
        //                       wx.navigateBack({
                                
        //                       });
        //                   }
        //                 })
        //               } else {
        //                 wx.showToast({
        //                   title: "支付失败",
        //                   icon: 'none'
        //                 })
        //               }
        //             },
        //           })
        //         }
        //       })
        //     } else if (res.data.result == 1 && res.data.data.orderStatus == 7001) {
        //       wx.showToast({
        //         title: '支付成功',
        //         icon: 'success',
        //         duration: 2000,
        //         success: function () {
        //           console.log('haha');
        //           setTimeout(function () {
        //             wx.navigateBack({
                      
        //             });
        //           }, 2000) //延迟时间
        //         }
        //       })
        //     }
        //   },
        // })
      }
    } else {
      wx.navigateTo({
        url: '/pages/cashierIntegral/cashierIntegral?userId=' + userId + '&username=' + that.data.username + '&telephone=' + that.data.phone + '&address=' + that.data.address + '&remark=' + that.data.remark + '&productIdAndNumList=' + that.data.productIdAndNumList + '&type=' + 4 + '&payChannel=' + 5003 + '&orderId' + that.data.orderId + '&ptype=' + that.data.type
      })
        // wx.request({
        //   url: app.globalData.url + 'getOrderWeixinPrepayId',
        //   data: {
        //     userId: wx.getStorageSync("userId"),
        //     orderId: that.data.orderId,
        //     orderNumber:0
        //   },
        //   method: "POST",
        //   header: {
        //     'content-type': 'application/x-www-form-urlencoded'
        //   },
        //   success: function (res) {
        //     var resultData = res.data;
        //     var hisId = resultData.hisId;
        //     if (res.data.result != 1 && res.data.result != 9) {
        //       wx.showToast({
        //         title: "数据异常，请稍后重试",
        //         icon: 'none'
        //       })
        //       return;
        //     }
        //     wx.requestPayment({
        //       'timeStamp': resultData.timeStamp,
        //       'nonceStr': resultData.nonceString,
        //       'package': resultData.packageStr,
        //       'signType': resultData.signType,
        //       'paySign': resultData.sign,
        //       //支付成功
        //       success: function (res) {
        //         if ("requestPayment:ok" === res.errMsg) {
        //           wx.showToast({
        //             title: '支付成功',
        //             icon: 'success',
        //             duration: 2000,
        //             success: function () {
        //               setTimeout(function () {
        //                 wx.navigateBack({
                         
        //                 });
        //               }, 2000) //延迟时间
        //             }
        //           })
        //         }
        //       },
        //       //支付或者取消失败
        //       fail: function (res) {
        //         if ("requestPayment:fail cancel" === res.errMsg) {
        //           wx.showToast({
        //             title: '取消支付',
        //             icon: 'none',
        //             duration: 2000,
        //             success: function () {
        //               setTimeout(function () {
        //                 wx.navigateBack({
                          
        //                 });
        //               }, 2000) //延迟时间
        //             }
        //           })
        //           wx.navigateBack({
                    
        //           });
        //         } else {
        //           wx.showToast({
        //             title: "支付失败",
        //             icon: 'none'
        //           })
        //         }
        //       },
        //     })
        //   }
        // })
    }
    
  },
  trueGet: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否已经收到货物',
      success: function (sm) {
        if (sm.confirm) {
          wx.request({
            url: app.globalData.url + 'orderSigned',
            method: 'GET',
            data: {
              orderId: that.data.orderId,
              userId: wx.getStorageSync("userId"),
              orderProductId: that.data.opid
            },
            success: function (res) {
              wx.navigateBack({
                
              })
            }
          });
        }
      }
    });
  },
  copyText: function (e) {
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '已复制快递单号到剪贴板'
            })
          }
        })
      }
    })
  },
})